package com.lkd.web.contorller;

import com.lkd.entity.Goods;
import com.lkd.entity.Users;
import com.lkd.mapper.GoodsMapper;
import com.lkd.mapper.GoodsOrderMapper;
import com.lkd.service.IGoodsService;
import com.lkd.service.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/goods")
public class GoodsController{


    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private IGoodsService goodsService;

//查询商品列表
    @RequestMapping("/list")
    public List<Goods> queryAll() {
        return goodsService.queryAll();
    }

//通过goodsId查询商品列表(查)
    @GetMapping("/goodsList")
    public @ResponseBody Goods goodsList(Long goodsId){
//        return goodsMapper.selectByPrimaryKey(goodsId);
        //http://localhost:8001/goods/goodsList?goodsId=1
        Goods goods = goodsService.queryGoodsById(goodsId);
        return goods;
    }

//通过goodsName查询商品列表(查)
    @GetMapping("/goodNameList")
    public @ResponseBody Goods goodNameList(String goodsName){
        return goodsMapper.selectByGoodName(goodsName);
        //http://localhost:8001/goods/goodNameList?goodsName=芝士雪豹
    }


//查询订单信息并级联查询出相应的明细信息(查)
    @Autowired
    private GoodsOrderMapper goodsOrderMapper;

    @GetMapping("/goodsOrderList")
    public @ResponseBody Object goodsOrderList(Integer orderId){
        return goodsOrderMapper.selectOrderAndOrderDetail(orderId);
        //http://localhost:8001/goods/goodsOrderList?orderId=1
    }

//上传商品(增)
    @PostMapping("/addGood")
    public ResponseResult addGood(@RequestBody Goods arg){
        Goods goods = goodsService.checkGood(arg);
        ResponseResult result = null;
        if (Objects.isNull(goods)){
            goodsService.addGood(arg);
            System.out.println("上传成功");
            result = new ResponseResult<>(HttpStatus.OK.value(), "上传成功", goods);
        }else {
            System.out.println("重复上传");
            result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "重复上传");
        }
        return result;
    }

//商品删除(删)
    @PostMapping("/deleteGood")
    public ResponseResult deleteGood(@RequestBody Goods arg){
        Goods goods = goodsService.checkGood(arg);
        ResponseResult result = null;
        if (Objects.isNull(goods)){
            System.out.println("没有该商品");
            result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "没有该商品");
        }else {
            goodsService.deleteGood(goods);
            System.out.println("删除成功");
            result = new ResponseResult<>(HttpStatus.OK.value(), "删除成功", goods);
        }
        return result;
    }

//修改商品(改)
    @PostMapping("updateGood")
    public ResponseResult updateGood(@RequestBody Goods arg){
        Goods goods = goodsService.checkGood(arg);
        ResponseResult result =null;
        if ( goodsService.updateGood(arg)==1){
            System.out.println("修改成功");
            result = new ResponseResult<>(HttpStatus.OK.value(), "修改成功", goods);
        }else {
            System.out.println("修改失败");
            result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "修改失败");
        }
        return result;
    }

//库存减一
    @GetMapping("/downInventory")
    public @ResponseBody ResponseResult downInventory(Integer goodsId){
        ResponseResult result =null;
        Goods goods =new Goods();
        goods.setGoodsId(goodsId);
        Goods newgoods = goodsService.checkGood(goods);
        if (Objects.isNull(newgoods)){
            System.out.println("不存在该商品");
            result = new ResponseResult<>(HttpStatus.FORBIDDEN.value(), "不存在该商品");
        }else {
            if(newgoods.getInventory()>0 ) {
                goodsService.downInventory(goodsId);
                System.out.println("商品库存减一成功");
                result = new ResponseResult<>(HttpStatus.OK.value(), "商品库存减一成功", newgoods);
            }else {
                System.out.println("该商品库存已为零");
                result = new ResponseResult<>(HttpStatus.NO_CONTENT.value(), "该商品库存已为零");
            }
        }
        return result;
    }

}
